package com.cmlite.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;

public class SideBar extends View {
	private char[] l;
	private SectionIndexer sectionIndexter = null;
	private ListView list;
	private int m_nItemHeight = 0;
	private float f_nItemHeight = 0;
	private int height = 0;

	public SideBar(Context context) {
		super(context);
		init();
	}

	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
				'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z' };
		setBackgroundColor(0x44c4bcbc);
	}

	public SideBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public void setListView(ListView _list, RelativeLayout relative, char[] _l) {
		list = _list;
		l = _l;
		Log.e("SON", "size: " + l.length);
		sectionIndexter = (SectionIndexer) _list.getAdapter();
		height = relative.getHeight();
		System.out.println("height: " + height);
		m_nItemHeight = height / l.length;
		this.invalidate();
	}

	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		int i = (int) event.getY();
		int idx = i / m_nItemHeight;
		if (idx >= l.length) {
			idx = l.length - 1;
		} else if (idx < 0) {
			idx = 0;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN
				|| event.getAction() == MotionEvent.ACTION_MOVE) {
			if (sectionIndexter == null) {
				sectionIndexter = (SectionIndexer) list.getAdapter();
			}
			int position = sectionIndexter.getPositionForSection(l[idx]);
			if (position == -1) {
				return true;
			}
			list.setSelection(position);
		}
		return true;
	}

	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		// paint.setColor(0xFFA6A9AA);
		paint.setColor(0xFF383838);
		paint.setTextSize((float) height / 26);
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		paint.setTextAlign(Paint.Align.CENTER);

		float widthCenter = getMeasuredWidth() / 2;
		for (int i = 0; i < l.length; i++) {
			canvas.drawText(String.valueOf(l[i]), widthCenter,
					((f_nItemHeight / 2) + 10 + (i * f_nItemHeight)), paint);
//			canvas.drawText(String.valueOf(l[i]), widthCenter, f_nItemHeight
//					+ (i * f_nItemHeight), paint);
			// canvas.drawText(String.valueOf(l[i]), widthCenter,
			// (i * f_nItemHeight), paint);
			// Log.e("SON", i + ": " + ((f_nItemHeight/2) + (i *
			// f_nItemHeight)));
		}
		super.onDraw(canvas);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onSizeChanged(int, int, int, int)
	 */
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		height = h;
		System.out.println("height: " + height + "l.lenght :" + l.length);
		m_nItemHeight = (height / l.length)
				+ ((h - ((height / l.length) * l.length)) / l.length);
		f_nItemHeight = (float) height / l.length;
		System.out.println("m_nItemHeight: " + m_nItemHeight + "\n"
				+ (float) (height / l.length) + "\nlength: " + l.length
				+ "\nf_nItemHeight: " + f_nItemHeight + "\nAAA: "
				+ (float) height / l.length);
	}

	@Override
	public void invalidate() {
		// TODO Auto-generated method stub
		super.invalidate();
	}

}